<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>邮件发送</title>
</head>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<body>
<form>
  <h1>邮箱登录</h1>
  用户名:<input type="text" name="username"><br>
  邮&nbsp;&nbsp;&nbsp;&nbsp;箱:<input type="text" name="email">
  <div style="display: flex">
    验证码:<input type="text" name="code" width="80%">
    <input type="button" name="username" width="20%" value="发送" onclick="send()">
  </div>
  <input type="button" name="sub" value="登录" onclick="login()">
</form>
</body>
<script>
  function send(){
    var username = $("input[name='username']").val();
    var email = $("input[name='email']").val();
    if(username.length<=0||email.length<=0||isEmail(email)){
      alert("姓名和邮箱不能为空！");
    }else{
      $.post('sendEmail.do',{
        'username':username,
        'email':email
      },function (res){
        alert(res);
      })
    }
  }
  function login(){
    var username = $("input[name='username']").val();
    var email = $("input[name='email']").val();
    var code =  $("input[name='code']").val();
    $.post('loginByEmail.do',{
      "username":username,
      "email":email,
      "code":code
    },function (res){
      alert(res);
      if(res==='登陆成功'){
        location.href = "index.jsp"
      }
    })
  }
  function isEmail(){
    return false;
  }
</script>
</html>